import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder';
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
import { DefaultStage } from '../default-stage';
import { Mesh } from '@babylonjs/core/Meshes/mesh';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { Helper } from 'babylon-lib/tool/helper';
import { LocationImage } from 'babylon-lib/texture/icon-image/location-image';
import { Texture } from '@babylonjs/core/Materials/Textures/texture';
import { StandardMaterial } from '@babylonjs/core';


/**
 * 选中标记
 */
class SelectedMark {

    private static _stage: DefaultStage;
    private static _container: TransformNode;
    private static _markMesh: Mesh;

    public static offset: Vector3 = new Vector3();

    public static setup(stage: DefaultStage): void {
        this._stage = stage;
        this._container = new TransformNode('selectedMarkContainer', this._stage.mainScene);
        this.initializeMesh();
    }

    public static attach(target?: TransformNode, offset?: Vector3): void {
        if (target) {
            this._container.parent = target;
            this._container.position.copyFrom(offset || new Vector3(0, 0, 0));
            Helper.getChildMeshes(this._container).forEach(m => m.isVisible = true);
        } else {
            this._container.parent = null;
            Helper.getChildMeshes(this._container).forEach(m => m.isVisible = false);
        }
    }

    private static initializeMesh(): void {
        const m = MeshBuilder.CreatePlane('selectedMarkMesh', { size: 1 }, this._stage.mainScene);
        const img = LocationImage(1, '#ffffff');
        const texture = new Texture(img);
        const material = new StandardMaterial('selectedMarkMaterial', this._stage.mainScene);
        material.disableLighting = true;
        material.transparencyMode = StandardMaterial.MATERIAL_ALPHATEST;
        material.emissiveTexture = texture;
        m.material = material;
        m.isPickable = false;
        this._markMesh = m;
    }

}


export { SelectedMark };